from django.shortcuts import render,redirect,HttpResponse
from application.models import caseInfo,caseDetail,projectInfo,projectAndCaseRelations
from django.db import transaction

@transaction.atomic
def addCaseTitlePage(request):
    context = {}
    caseInfos = caseInfo.objects.all().order_by("position");
    context['caseInfos'] = caseInfos
    return render(request,"admin/addCaseTilePage.html",context);


def addCaseTitle(request):
    title = request.POST.get('title');
    exist = caseInfo.objects.filter(caseTitle=title);
    position = request.POST.get('position');
    if len(exist) > 0:
        return redirect('/addCaseTitlePage/');
    caseInfo(caseTitle=title,position=position).save();
    newCase = caseInfo.objects.get(caseTitle=title);
    projects = projectInfo.objects.all();
    for project in projects:
        projectAndCaseRelations(projectId=project.id,caseId=newCase.id,status='1').save();
    return redirect('/addCaseTitlePage/');

@transaction.atomic
def modifyCaseTitle(request):
    caseId = request.POST.get('caseId');
    caseTitle = request.POST.get('caseTitle');
    result = 1;
    try:
        case = caseInfo.objects.get(id=caseId);
        case.caseTitle = caseTitle;
        case.save();
    except Exception:
        result = 1;
    return HttpResponse(result);

@transaction.atomic
def addCaseDetailPage(request):
    caseId = request.POST.get('caseId');
    case = caseInfo.objects.filter(id=caseId)[0];
    context = {}
    context['caseInfo'] = case;
    return render(request,'admin/addCaseDetailPage.html',context)


@transaction.atomic
def addCaseDetail(request):
    result = 1;
    try:
        caseId = request.POST.get('caseId');
        caseContent = request.POST.get('caseContent');
        caseDetailName = request.POST.get('caseDetailName');
        caseContent = caseContent.replace("\n","<br>")
        caseDetail(caseTitleId=caseId,caseDetailTitle=caseDetailName,caseContent=caseContent).save();
    except Exception:
        result = 0;
    return HttpResponse(result);


@transaction.atomic
def checkCaseDetail(request):
    caseTitleId = request.POST.get('caseTitleId');
    caseDetails = caseDetail.objects.get(id=caseTitleId);
    caseInfos = caseInfo.objects.filter(id=int(caseDetails.caseTitleId))[0]
    context = {};
    context['caseDetail'] = caseDetails;
    context['caseInfo'] = caseInfos;
    return render(request,'admin/checkCaseDetail.html',context);


@transaction.atomic
def checkCaseDetailName(request):
    caseId = request.POST.get('caseId');
    caseDetails = caseDetail.objects.filter(caseTitleId=caseId).order_by('position');
    aCaseInfo = caseInfo.objects.filter(id=caseId)[0];
    context = {}
    context['caseDetails'] = caseDetails;
    context['caseInfo'] = aCaseInfo;
    return render(request,'admin/caseDetailNameList.html',context);



@transaction.atomic
def modifyCaseAndCaseDetailBind(request):
    id = request.POST.get('id');
    status = request.POST.get('status');
    result = 1;
    try:
        caseDetails = caseDetail.objects.get(id=id);
        caseDetails.status = status;
        caseDetails.save();
    except Exception:
        result = 0;
    return HttpResponse(result);



@transaction.atomic
def modifyCaseDetailPage(request):
    id = request.POST.get('caseDetailId');
    caseDetailInfo = caseDetail.objects.get(id=id);
    caseDetailInfo.caseContent = caseDetailInfo.caseContent.replace("<br>","\n");
    caseInfos = caseInfo.objects.get(id=int(caseDetailInfo.caseTitleId));
    context = {}
    context['caseDetail']=caseDetailInfo;
    context['caseInfo'] = caseInfos;
    return render(request,"admin/modifyCaseDetailPage.html",context);



@transaction.atomic
def modifyCaseDetail(request):
    result = 1;
    try:
        caseDetailId = request.POST.get('caseDetailId');
        caseContent = request.POST.get('caseContent');
        caseDetailName = request.POST.get('caseDetailName');
        caseContent = caseContent.replace("\n", "<br>")
        caseDetailInfo = caseDetail.objects.get(id=caseDetailId);
        caseDetailInfo.caseContent=caseContent
        caseDetailInfo.caseDetailTitle = caseDetailName
        caseDetailInfo.save();
    except Exception:
        result = 0;
    return HttpResponse(result);


@transaction.atomic
def changePosition(request):
    result = 1;
    try:
        position = request.POST.get('position');
        caseId = request.POST.get('caseId');
        case = caseInfo.objects.get(id=caseId);
        case.position = position;
        case.save();
    except Exception:
        result = 1;
    return HttpResponse(result);



@transaction.atomic
def changeCaseDetailPosition(request):
    result = 1;
    try:
        posistion = request.POST.get('position');
        caseDetailId = request.POST.get('caseDetailId');
        aCaseDetail = caseDetail.objects.get(id=caseDetailId);
        aCaseDetail.position = posistion
        aCaseDetail.save();
    except Exception:
        result = 0;
    return HttpResponse(result);